package com.albert.thread;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import static com.albert.thread.ThreadPool.service;
/*
* 串行化
* */
public class AsyncFunction {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("开始");
        //1.thenRun 不能获取上一步的返回结果
        /*CompletableFuture<Void> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("开始执行异步方法一");
            return 10 / 2;
        }, service).thenRunAsync(()->{
            System.out.println("方法二启动");
        },service);*/
        //2.能接受上一步结果,但是没有返回值
        /*CompletableFuture<Void> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("开始执行异步方法一");
            return 10 / 2;
        }, service).thenAcceptAsync(res->{
            System.out.println("方法二启动获取结果:"+res);
        },service);*/
        //3.thenApplyAsync  接收上一步结果过并且有返回值
        CompletableFuture<String> future3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("开始执行异步方法一");
            return 10 / 2;
        }, service).thenApplyAsync(res->{
            System.out.println("方法二启动"+res);
            return "hello"+res;
        },service);
        System.out.println("结束"+future3.get());
    }
}
